package com.qk.controller;

import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户列表查询
     */
    @GetMapping
    public Result list(UserDto userDto){
        log.info("用户列表查询: {}" , userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }


    //新增

    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户: {}", user);
        userService.addUser(user);
        return Result.success();
    }

    //删除
@DeleteMapping("/{id}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除用户: {}", ids);
        userService.deleteByIds(ids);
        return Result.success();
}

    /**
     * 根据ID查询用户信息
     *
     * @param id 用户ID
     * @return 统一响应结果
     */
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        log.info("根据ID查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     *
     * @param user 用户信息
     * @return 统一响应结果
     */
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.updateUser(user);
        return Result.success();
    }

}
